
class Signal {
    constructor() {
        this.signals = {};
    }
    
    register(signalName, func) {
        if (this.signals[signalName] == null) {
            this.signals[signalName] = [];
        }
        this.signals[signalName].push(func);
    }

    emit(signalName, ...args) {
        var funcList = this.signals[signalName];
        if (funcList) {
            for (let i = 0; i < funcList.length; ++i) {
                var func = funcList[i];
                if (func) {
                    func(...args);
                }
            }
        }
    }

    unregister(signalName, func) {
        var funcList = this.signals[signalName];
        if (funcList) {
            var startPos = funcList.indexOf(func);
            funcList.splice(startPos, 1);
        }
    }
}

// var signals = {}

// exports.register = function(signalName, func) {
//     if (signals[signalName] == null) {
//         signals[signalName] = [];
//     }
//     signals[signalName].push(func);
// }

// exports.emit = function(signalName, ...args) {
//     var funcList = signals[signalName];
//     if (funcList) {
//         for (let i = 0; i < funcList.length; ++i) {
//             var func = funcList[i];
//             if (func) {
//                 func(...args);
//             }
//         }
//     }
// }

// exports.unregister = function(signalName, func) {
//     var funcList = signals[signalName];
//     if (funcList) {
//         var startPos = funcList.indexOf(func);
//         funcList.splice(startPos, 1);
//     }
// }

const { SIGNAL_DEFINE } = require("./signalDefines");
exports.SIGNAL_DEFINE = SIGNAL_DEFINE;
exports.Signal = Signal;